//
//  JPushManager.m
//  CRM
//
//  Created by teeking_scx on 2018/1/26.
//  Copyright © 2018年 teeking_scx. All rights reserved.
//

#import "JPushManager.h"
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
@interface JPushManager()<JPUSHRegisterDelegate>

@end
static JPushManager *_instance;
@implementation JPushManager
+ (instancetype)manager{
    if (_instance == nil) {
        _instance = [[JPushManager alloc] init];
    }
    return _instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

-(void)initJPush:(NSDictionary *)launchOptions{
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#ifdef DEBUG
    [JPUSHService setupWithOption:launchOptions appKey:@"396feb93d1cb6f6068b0bca1" channel:@"App Store" apsForProduction:NO advertisingIdentifier:nil];
#else
    [JPUSHService setupWithOption:launchOptions appKey:@"396feb93d1cb6f6068b0bca1" channel:@"App Store" apsForProduction:YES advertisingIdentifier:nil];
#endif

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
}
-(void)registerDeviceToken:(NSData *)deviceToken{
    [JPUSHService registerDeviceToken:deviceToken];
}
-(void)networkDidReceiveMessage:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    NSString *content = [userInfo valueForKey:@"content"];
    NSLog(@"%@",content);
}
-(void)handleRemoteNotification:(NSDictionary *)userInfo{
    [JPUSHService handleRemoteNotification:userInfo];
}
-(void)clearBadge{
    [JPUSHService setBadge:0];
}
-(void)clearPush{
    [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
    } seq:1000];
    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
    } seq:1001];
}

-(void)jpushSetAlias:(NSString *)alias{
    [JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {} seq:100];
}

#pragma mark -- JPUSHRegisterDelegate

-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if (@available(iOS 10.0, *)) {
        if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            /// iOS10处理远程推送
            [JPUSHService handleRemoteNotification:userInfo];
            
//            UIWindow *window = KeyWindow;
//            UIViewController *viewCtrl = window.rootViewController;
//            if ([viewCtrl isKindOfClass:[TabBarController class]]) {
//                TabBarController *tabBar = (TabBarController *)viewCtrl;
//                [tabBar setBadge];
//            }
            /// 应用处于前台收到推送的时候转成本地通知 ===========================
//            [[ZAlertViewManager shareManager] showWithType:AlertViewTypeMessage title: userInfo[@"aps"][@"alert"]];
        }
    } else { 
        completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
    }
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if (@available(iOS 10.0, *)) {
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
    }
    completionHandler();
}

@end
